package com.da.goal.controller;

import com.alibaba.fastjson.JSON;
import com.da.common.enums.ResultEnum;
import com.da.common.exception.ParameterException;
import com.da.common.utils.DailyAimResult;
import com.da.goal.convert.GoalCategoriesConverter;
import com.da.goal.entity.DailyGoalCategories;
import com.da.goal.entity.dto.DailyGoalsCategoriesDTO;
import com.da.goal.entity.vo.DailyCategoriesTagVO;
import com.da.goal.service.DailyGoalsCategoriesService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-02-13
 * @Version: 1.0
 * <p>
 * 目标分类controller 负责分类标签项管的api
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/dailyGoalCategories")
public class DailyGoalCategoriesController {

    private final DailyGoalsCategoriesService dailyGoalsCategoriesService;

    /**
     * 创建目标分类api
     *
     * @param dailyGoalsCategoriesDTO 目标分类dto
     * @return
     */
    @PostMapping("/createGoalCategories")
    @Operation(summary = "创建目标分类", description = "创建目标分类")
    public DailyAimResult<?> createGoalCategories(@RequestBody DailyGoalsCategoriesDTO dailyGoalsCategoriesDTO) {
        log.info("DailyGoalCategoriesController.createGoalCategories.dailyGoalsCategoriesDTO:{}", JSON.toJSONString(dailyGoalsCategoriesDTO));
        Optional.ofNullable(dailyGoalsCategoriesDTO).orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "目标分类信息不能为空"));
        DailyGoalCategories dailyGoalCategories = GoalCategoriesConverter.INSTANCE.dtoToEntity(dailyGoalsCategoriesDTO);
        boolean isCreateDailyGoalCategories = dailyGoalsCategoriesService.createGoalCategories(dailyGoalCategories);
        if (isCreateDailyGoalCategories) {
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "创建目标分类成功！");
        }
        return DailyAimResult.error(ResultEnum.FAIL.getCode(), "创建目标分类失败！");
    }

    /**
     * 数状查询分类及对应分类下的标签
     * @return
     */
    @GetMapping("/treeQueryGoalCategoriesTagsInfo")
    @Operation(summary = "数状查询分类及对应分类下的标签", description = "数状查询分类及对应分类下的标签")
    public DailyAimResult<?> treeQueryGoalCategoriesTagsInfo(){
        List<DailyCategoriesTagVO> dailyCategoriesTagVOS = dailyGoalsCategoriesService.queryTreeGoalCategoriesInfo();
        return DailyAimResult.success(ResultEnum.SUCCESS.getCode(),"查询目标分类信息成功",dailyCategoriesTagVOS);
    }




}
